package com.hg.library.widget;

import android.animation.Animator;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.hg.library.utils.InputManager;


/**
 * Created by ASUS-PC on 2016/5/23.
 */
public abstract class CuteGallery extends RelativeLayout {

    protected Context mContext;

    protected Resources mResources;

    private boolean allowRequestFocued = true;

    protected CuteView tempCuteView;

    protected int displayCount = 7;

    protected AdapterView.OnItemClickListener onItemClickListener;
    protected AdapterView.OnItemSelectedListener onSelectItemListener;
    protected OnkeyListener onKeyListener1;

    protected CuteAdapter adapter;

    DataCuteObserver dataCuteObserver;

    public int ANIM_FLAG;

    protected LoopLinkList cacheList = getLoopLinkList();
    public Animator.AnimatorListener animatorListener;

    public CuteGallery(Context context) {
        super(context);
    }

    public CuteGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mResources = mContext.getResources();
    }

    public void displayCount() {
        initView();
    }

    private void initView() {

        if (getChildCount() >0){
            removeAllViews();
        }

        initLoaclPosition();
        for (int i = 0; i < getDisplayCount() + 1; i++) {
            View view = getInstanceView();
            if (getILayoutParams() == null) {
                LayoutParams layoutParams = new LayoutParams(-2, -2);
                layoutParams.addRule(CENTER_IN_PARENT);
                view.setLayoutParams(layoutParams);
            } else {
                view.setLayoutParams(getILayoutParams());
            }
            view.setOnKeyListener(onKeyListener);
            view.setOnClickListener(onClickListener);
            addView(view);
        }

        for (int i = 0; i < getDisplayCount() + 1; i++) {
            View view = getChildAt(i);
            if (view instanceof CuteView) {
                CuteView cuteView = (CuteView) view;
                if (i == 0) {
                    cuteView.setLeftView((CuteView) getChildAt(getDisplayCount()));
                    cuteView.setRightView((CuteView) getChildAt(i + 1));
                } else if (i == getDisplayCount()) {
                    cuteView.setLeftView((CuteView) getChildAt(i - 1));
                    cuteView.setRightView((CuteView) getChildAt(0));
                } else {
                    cuteView.setLeftView((CuteView) getChildAt(i - 1));
                    cuteView.setRightView((CuteView) getChildAt(i + 1));
                }
            } else {
                throw new IllegalArgumentException(view.getClass().getName() + "不是CuteView的实例");
            }
        }
    }

    private OnKeyListener onKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && event.getAction() == KeyEvent.ACTION_DOWN && allowRequestFocued) {
                if (onKeyListener1 != null) onKeyListener1.onKeyLeft(v);
                timer();
                onKeyLeft((CuteView) v);
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.getAction() == KeyEvent.ACTION_DOWN && allowRequestFocued) {
                if (onKeyListener1 != null) onKeyListener1.onKeyRight(v);
                timer();
                onKeyRight((CuteView) v);
            } else if (InputManager.handledEnterKeyEvent(keyCode) && event.getAction() == KeyEvent.ACTION_UP) {
                onItemClick(v, getSelectedPosition());
                playSoundEffect(SoundEffectConstants.CLICK);
            }

            if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                return false;
            }

            return true;
        }
    };

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            onItemClick(v, getSelectedPosition());
        }
    };

    Handler handler = new Handler();

    private void timer() {
        if (!getAllowRequestFocued()) return;
        setAllowRequestFocued(false);
        if (onScrollChangeListener != null) onScrollChangeListener.onScrollStart(this);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                setAllowRequestFocued(true);
                if (onScrollChangeListener != null)
                    onScrollChangeListener.onScrollEnd(CuteGallery.this);
            }
        }, 500);
        //mContext.getResources().getInteger(R.integer.duration)
    }

    protected void onItemSelect(View v, int position) {
       /* Log.e("onItemClick",getClass().getName()+"");*/
        if (onSelectItemListener != null)
            onSelectItemListener.onItemSelected(null, v, position, -1);
    }

    protected void onItemClick(View v, int position) {
       /* Log.e("onItemSelect",getClass().getName()+"");*/
        if (onItemClickListener != null) onItemClickListener.onItemClick(null, v, position, -1);
    }

    public void setAdapter(CuteAdapter adapter) {
        this.adapter = adapter;

        if (getChildCount() > 0) {
            removeAllViews();
            cacheList.clear();
        }

        if (adapter != null && dataCuteObserver != null)
            adapter.unregisterCuteObserver(dataCuteObserver);

        dataCuteObserver = new DataCuteObserver();

        adapter.registerCuteObserver(dataCuteObserver);

        initLoaclPosition();
        for (int i = 0; i < getDisplayCount() + 1; i++) {
            View view = adapter.onCreateView(i, null);
            if (!(view instanceof CuteView)) {
                throw new IllegalArgumentException(view.getClass().getName() + "不是" + CuteView.class.getName() + "的实例");
            }
            if (getILayoutParams() == null) {
                LayoutParams layoutParams = new LayoutParams(-2, -2);
                layoutParams.addRule(CENTER_IN_PARENT);
                view.setLayoutParams(layoutParams);
            } else {
                view.setLayoutParams(getILayoutParams());
            }
            view.setOnKeyListener(onKeyListener);
            view.setOnClickListener(onClickListener);
            addView(view);
            if (i == getDisplayCount()) continue;

            adapter.onHolderView(view, i, adapter.getItemData(i));
        }

        for (int i = 0; i < getDisplayCount() + 1; i++) {
            View view = getChildAt(i);
            CuteView cuteView = (CuteView) view;
            if (i == 0) {
                cuteView.setLeftView((CuteView) getChildAt(getDisplayCount()));
                cuteView.setRightView((CuteView) getChildAt(i + 1));
            } else if (i == getDisplayCount()) {
                cuteView.setLeftView((CuteView) getChildAt(i - 1));
                cuteView.setRightView((CuteView) getChildAt(0));
            } else {
                cuteView.setLeftView((CuteView) getChildAt(i - 1));
                cuteView.setRightView((CuteView) getChildAt(i + 1));
            }
        }
    }

    public CuteAdapter getAdapter() {
        return adapter;
    }

    class DataCuteObserver implements CuteObserver {

        @Override
        public void onChanged() {
            CuteGallery.this.onChanged();
        }
    }

    protected void onChanged() {
    }

    protected LoopLinkList getLoopLinkList() {
        return new LoopLinkList();
    }

    protected void setAllowRequestFocued(boolean b) {
        allowRequestFocued = b;
    }

    public void setAllowedScroll(boolean b) {
        allowRequestFocued = b;
    }

    public boolean getAllowedScroll() {
        return allowRequestFocued;
    }

    protected boolean getAllowRequestFocued() {
        return allowRequestFocued;
    }

    public abstract View getInstanceView();

    public abstract LayoutParams getILayoutParams();

    public abstract void onKeyLeft(CuteView v);

    public abstract void onKeyRight(CuteView v);

    public abstract View getSelectedView();

    public abstract void initLoaclPosition();

    public abstract int getSelectedPosition();

    protected int getDisplayCount() {
        return displayCount;
    }

    protected void setDisplayCount(int displayCount) {
        this.displayCount = displayCount;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnSelectItemListener(AdapterView.OnItemSelectedListener onSelectItemListener) {
        this.onSelectItemListener = onSelectItemListener;
    }

    public void setOnKeyEventListener(OnkeyListener onKeyListener) {
        onKeyListener1 = onKeyListener;
    }

    protected OnScrollChangeListener onScrollChangeListener;

    public void setOnScrollChangeListener(OnScrollChangeListener onScrollChangeListener) {
        this.onScrollChangeListener = onScrollChangeListener;
    }

    public interface OnScrollChangeListener {
        void onScrollStart(CuteGallery cuteGallery);

        void onScrollEnd(CuteGallery cuteGallery);
    }

    public void setAnimatorListener(Animator.AnimatorListener animatorListener) {
        this.animatorListener = animatorListener;
    }

    protected void registerAnimListener(Animator animatorSet) {
        if (ANIM_FLAG == 0) {
            animatorSet.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    ANIM_FLAG++;
                    if (animatorListener != null) animatorListener.onAnimationStart(animation);
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    ANIM_FLAG--;
                    if (animatorListener != null) animatorListener.onAnimationEnd(animation);
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    if (animatorListener != null) animatorListener.onAnimationCancel(animation);
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                    if (animatorListener != null) animatorListener.onAnimationRepeat(animation);
                }
            });
        }
    }

    public interface OnkeyListener {
        void onKeyLeft(View v);

        void onKeyRight(View v);
    }

}
